<?php
/**
 * @version		$Id: hosting_plan.php 8 2011-12-23 15:25:34Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modeladmin');

class AlmHosterModelHosting_plan extends JModelAdmin
{
	
	
	public function getTable($type = 'Hosting_plans', $prefix = 'AlmHosterTable', $config = array()) 
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	
	public function getForm($data = array(), $loadData = true) 
	{
		
		$form = $this->loadForm('com_almhoster.hosting_plan', 'hosting_plan', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) 
		{
			return false;
		}
		return $form;
	}

	
	protected function loadFormData() 
	{

		$data = JFactory::getApplication()->getUserState('com_almhoster.edit.hosting_plan.data', array());
		if (empty($data)) 
		{
			$data = $this->getItem();
		}
		return $data;
	}
		
	public function update_plans() 
	{	
		$app = JFactory::getApplication();
		$pk = JRequest::getInt('PlanId');
		$db = JFactory::getDBO();
		$query	= $db->getQuery(true);
		$query->select('*');
		$query->from('#__almh_hostingplans');
		$query->where('PlanId = '.$pk);
		$db->setQuery($query);
		$db->setQuery((string) $query);
		
			if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}	
			$plan = $db->loadObject();
	 		
	 		$plan_data = curldata($plan->PlanServer,plan_data());
			$data = new SimpleXMLElement($plan_data);
	 		
			foreach ($data->package as $future){
				$query = "UPDATE #__almh_hostingplans SET PlanQuota = '$future->QUOTA', PlanBandwidth = '$future->BWLIMIT',
						PlanAddonDomains = '$future->MAXADDON', PlanFTPAcc = '$future->MAXFTP', PlanParkedDomains = '$future->MAXPARK',
						PlanSubDomains = '$future->MAXSUB', PlanMaxDatabases = '$future->MAXSQL', PlanEmailAcc = '$future->MAXPOP',
						PlanEmailLists = '$future->MAXLST'  WHERE PlanName = '$future->name' AND PlanId = '$pk'";
				$db->setQuery( $query );
				$db->query();
			}
		return true;
	}	
}